package services;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import DataAccess.CayThaoLuanDA;
import DataAccess.NguoiDungDA;
import DataAccess.NhomDA;
import DataAccess.NodeThaoLuanDA;

import pojo.CayThaoLuanPOJO;
import pojo.NguoiDungPOJO;
import pojo.NhomPOJO;
import pojo.NodeThaoLuanPOJO;
import pojo.PairValue;
import util.ClientSender;
import util.Common;
import util.XMLBuidler;

/**
 * Servlet implementation class CayThaoLuanServlet
 */
@WebServlet("/CayThaoLuanServlet")
public class CayThaoLuanServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CayThaoLuanServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out=response.getWriter();
		
		String cmd=getParameter(request, Protocol.CMD);
		CayThaoLuanDA cda=new CayThaoLuanDA();
		if(cmd.equals(Protocol.TAO_CAY)){
			String idNguoiTao=getParameter(request, Protocol.ID_NGUOI_DUNG);
			String noiDung=getParameter(request, Protocol.NOI_DUNG);
			String idNhom=getParameter(request, Protocol.ID_NHOM);
			
			CayThaoLuanPOJO cay=new CayThaoLuanPOJO();
			cay.setIdNguoiTao(Integer.parseInt(idNguoiTao));
			cay.setIdNhom(Integer.parseInt(idNhom));
			cay.setNoiDung(noiDung);
			
			
			int idCay=cda.insert(cay);
			cay.setId(idCay);
			out.print(idCay);
			
		}else if(cmd.equals(Protocol.LAY_DANH_SACH_CAY)){
			int idNguoiDung= Integer.parseInt( getParameter(request, Protocol.ID));
			
			ArrayList<NhomPOJO> lstNhom=new NhomDA().getNhomNguoiDung(idNguoiDung);
			
			Document doc=XMLBuidler.BuildXML("Root");
			if(lstNhom!=null){
				for(NhomPOJO nhom:lstNhom){
					Element element=nhom.createContactXMLElement(doc);
					ArrayList<CayThaoLuanPOJO> lstCay=new CayThaoLuanDA().getByNhom(nhom.getId());
					for(CayThaoLuanPOJO cay:lstCay){
						element.appendChild(cay.createXMLElement(doc));
					}
					doc.getFirstChild().appendChild(element);
				}
			}
			
			out.print(XMLBuidler.docToString(doc));			
			
		}else if(cmd.equals(Protocol.LAY_CAY)){
			String idCay=getParameter(request, Protocol.ID);
			
			CayThaoLuanPOJO cay=cda.get(Integer.parseInt(idCay));
			if(cay!=null){
				System.out.println("Cay != null");
				out.print(XMLBuidler.taoCay(cay));
			}else{
				System.out.println("Cay bi null");
			}
		}
		else if(cmd.equals(Protocol.THEM_NODE)){
			String idNodeCha,idCay,idNguoiTao,noiDung,ghiChu;
			idNodeCha=getParameter(request, Protocol.ID);
			idCay=getParameter(request, Protocol.ID_CAY);
			idNguoiTao=getParameter(request, Protocol.ID_NGUOI_DUNG);
			noiDung=getParameter(request, Protocol.NOI_DUNG);
			ghiChu=getParameter(request, Protocol.GHI_CHU,"");
			NodeThaoLuanPOJO node=new NodeThaoLuanPOJO();
			node.setIdNodeCha(Integer.parseInt(idNodeCha));
			node.setIdCay(Integer.parseInt(idCay));
			node.setIdNguoiTao(Integer.parseInt(idNguoiTao));
			node.setNoiDung(noiDung);
			node.setGhiChu(ghiChu);
			
			NodeThaoLuanDA nda=new NodeThaoLuanDA();
			nda.insertNode(node);		
			out.print(XMLBuidler.nodeToXMLString(node));
			
			//Gửi thông tin cho các thành viên trong nhóm
			ArrayList<NguoiDungPOJO> lstNguoiDung=new NguoiDungDA().getNguoiDungCuaCay(Integer.parseInt(idCay));
			ArrayList<PairValue> params=new ArrayList<PairValue>();
			params.add(new PairValue(Protocol.CMD,Protocol.THEM_NODE));
			params.add(new PairValue(Protocol.ID_CAY,node.getIdCay()+""));
			params.add(new PairValue(Protocol.VERSION, node.getVersion()+""));
			
			try {
				ArrayList<String> lstRegId=Common.retrieveRegId(lstNguoiDung);
				ClientSender.send(lstRegId, params, 30);
			} catch (Exception e) {
				System.out.println("Loi:"+e.getMessage());
			}
		}
		else if(cmd.equals(Protocol.XOA_NODE)){
			int idNode=Integer.parseInt(getParameter(request, Protocol.ID));
			int idCay=Integer.parseInt(getParameter(request, Protocol.ID_CAY));
			long rs=new NodeThaoLuanDA().deleteNode(idCay, idNode);
			out.print(rs);
		}
		else if(cmd.equals(Protocol.CAP_NHAT_NODE)){
			int idNode=Integer.parseInt(getParameter(request, Protocol.ID));
			int idCay=Integer.parseInt(getParameter(request, Protocol.ID_CAY));
			String noiDung=getParameter(request, Protocol.NOI_DUNG);
			String ghiChu=getParameter(request, Protocol.GHI_CHU);
			NodeThaoLuanPOJO node=new NodeThaoLuanPOJO();
			node.setId(idNode);
			node.setIdCay(idCay);
			node.setGhiChu(ghiChu);
			node.setNoiDung(noiDung);
			long rs=new NodeThaoLuanDA().update(node);
			out.print(rs);
			
			//Gửi thông tin cho các thành viên trong nhóm
			ArrayList<NguoiDungPOJO> lstNguoiDung=new NguoiDungDA().getNguoiDungCuaCay(idCay);
			ArrayList<PairValue> params=new ArrayList<PairValue>();
			params.add(new PairValue(Protocol.CMD,Protocol.CAP_NHAT_NODE));
			params.add(new PairValue(Protocol.ID_CAY,node.getIdCay()+""));
			params.add(new PairValue(Protocol.VERSION, rs+""));
			try {
				ArrayList<String> lstRegId=Common.retrieveRegId(lstNguoiDung);
				ClientSender.send(lstRegId, params, 30);
			} catch (Exception e) {
				System.out.println("Loi:"+e.getMessage());
			}
		}
		else{
			System.out.println(cmd);
		}
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
